import Vue from "vue";
import Router from "vue-router";

import ChildA from "../components/childA";
import ChildB from "../components/childB";

import Admin from "../components/admin";
import Login from "../components/login";

Vue.use(Router);

let router = new Router({
  routes: [
    {
      path: "/",
      component: Admin,
      children: [
        {
          path: "a",
          name: "childA",
          component: ChildA
        },
        {
          path: "b",
          name: "childB",
          component: ChildB
        }
      ]
    },

    {
      path: "/login",
      component: Login,
      name: Login
    }
  ]
});

//全局前置路由守卫
router.beforeEach((to, from, next) => {
  var flag = window.localStorage.getItem("isLogin");
  if (flag) {
    next();
  } else if (to.path == "/login") {
    next();
  } else {
    next("/login");
  }
});

export default router;
